To split the given string files_str into a list of file names in Python, you can use the splitlines() method, which splits a string into a list where each line is a list item. Additionally, you might want to strip any leading or trailing whitespace from each line, which can be done using the strip() method. Here is how you can do it:

files_str = '''
Page1.md
Page2.md
    '''

# Split the string by lines and strip whitespace
file_names = [line.strip() for line in files_str.splitlines() if line.strip()]

print(file_names)

Breakdown of the Code:

  1. files_str.splitlines(): This method splits the string files_str at line boundaries. The result is a list of lines in the string, including any blank lines.

  2. line.strip(): This is used in a list comprehension to remove any leading or trailing whitespace from each line. This includes spaces and newline characters.

  3. if line.strip(): This condition in the list comprehension ensures that only non-empty lines are included in the final list file_names. It effectively filters out any lines that, once stripped of whitespace, are empty.

The final output of this script will be:

['Page1.md', 'Page2.md']

This list contains the cleaned file names, each as a separate string in the list.

new word count: 6
Word